# Library of functions to interface with the AA2TX iRotor TSR and command-line
# program


proc testForRotorDRV {} { 
    global iRotorExec
     set out [exec $iRotorExec  "status"]
    puts "test result: $out"
    if {$out == "RotorDRV is not loaded"} {
	return false
    } else {
	return true
    }
}

proc startRotorDRV {} {
     	 global iRotorDr
	 global stopPole
	 global rotorMode
	 global rotorParallelPortNumber   
	 puts "setup rotorDRV start: iRotorDR: $iRotorDr , stopPole: $stopPole , rotorMode: $rotorMode , rotorPPNum: $rotorParallelPortNumber"
    if {[testForRotorDRV] == false} {
    set out [exec $iRotorDr $rotorParallelPortNumber $stopPole $rotorMode]  
    puts "Started RotorDRV: $out"
    return "Started RotorDRV: $out"
    } else {
	puts "RotorDRV already running"
	return "RotorDRV already running"
    }
}

proc setRotor {az el} {
    global iRotorExec
    set out [exec $iRotorExec "position" $az $el]
    set out [exec $iRotorExec "r"]
    puts $out
}

proc startiRotor {} {
    global iRotorExec
    global flip
    set out [exec $iRotorExec "track" "on"]
    puts $out
    set out [exec $iRotorExec "flip"  $flip]
    set out [exec $iRotorExec "status"]
    puts $out
}

proc processElements {az el} {
	setRotor [expr round ($az)] [expr round ($el)]
}
